﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BulletinModel;
using System.Web.Mvc;
namespace BulletinMvc.Lib
{
    public class FeedbackBL:IFeedbackBL
    {
        private BulletinDBEntities db;
        private ModelStateDictionary _errg;

        public FeedbackBL(ModelStateDictionary ModelState) 
        {
            db = new BulletinDBEntities();
            _errg = ModelState;
    
        }
        public int GetCommentCountByprojectSN(string projectSN) 
        {
            return (from a in db.Feedbacks where a.ProjectSN == projectSN select a).Count();
            //db.Feedbacks.Select(a => a.ProjectSN == projectSN).Count();   
        }
        public decimal GetAverageStarvalueByprojectSN(string projectSN) 
        {
            decimal averagestarvalue;
          int toalstarvalue=0;
          float  i=0;
          db.Feedbacks.Where(a=>a.ProjectSN==projectSN).ToList().ForEach(a=>
          {
              if (a.StarValue !=0) {
                  toalstarvalue += Convert.ToInt16(a.StarValue);
                  i++;
              
              };
          });
          if (toalstarvalue == 0)
          {
              decimal aa = 0;
              return aa;

          }
          else {
              averagestarvalue = Convert.ToDecimal(toalstarvalue / i);

              return decimal.Round(averagestarvalue, 1);     
          
          }

           
        }

       public bool HaveCommentByprojectSN(string projectSN)
         {
             if ((from a in db.Feedbacks where a.ProjectSN==projectSN select a).Count() >0) return true;
             else return false;
         
         }
      public  void DeleteCommentByprojectSN(string projectSN) 
       {

           db.Feedbacks.Where(a => a.ProjectSN == projectSN).ToList().ForEach(a =>
          {
              db.Feedbacks.Remove(a);
          });
           db.SaveChanges();
       }
      public void AddComment(string projectSN, int StarValue, string TextValue) 
      {
          Feedback addfeedback = new Feedback 
          {
               ProjectSN=projectSN,
               StarValue = StarValue,
               TextValue = TextValue
                   
          };
          db.Feedbacks.Add(addfeedback);
          db.SaveChanges();
      
      }
   public   IQueryable<Feedback> GetFeedBackList(string projectSN)
      {
          return db.Feedbacks.OrderByDescending(a => a.ID).Where(b => b.ProjectSN == projectSN).Take(10);      
      }
     public IQueryable<Feedback> GetAllFeedBackList(string projectSN)
      {
          return db.Feedbacks.OrderByDescending(a => a.ID).Where(b => b.ProjectSN == projectSN);      
      }


        
   public bool DeleteCommentByID(int ID)
   {
   try{
       db.Feedbacks.Remove(db.Feedbacks.Find(ID));
       db.SaveChanges();
       return true;
             }catch{
       return false;
             }
       
   
   
   }


    public Feedback GetCommentByID(int ID) 
     {

         return db.Feedbacks.Single(a=>a.ID==ID);
   
     }


    }
}